import { getToken, setToken } from '@/utils/auth'
import { login, getUserInfo } from '@/api/user'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    setUserInfo(state, newInfo) {
      state.userInfo = newInfo
    }
  },
  actions: {
    async logout(context) {
      context.commit('setToken', '')
      context.commit('setUserInfo', {})
      // const res = await logout()
      // console.log(res)
      resetRouter()
    },
    async getUserInfo(context) {
      const res = await getUserInfo()
      // console.log('getProfile......', res.data)
      // res.data.roles.points = ['export-import', 'excel_export-btn']
      // console.log('getUserInfo......', res)
      context.commit('setUserInfo', res.data)
      return res.data
    },
    async login(context, payload) {
      console.log('login', context, payload)
      const res = await login(payload)
      console.log(res)
      const token = res.data
      context.commit('setToken', token)
    }
  }
}
